package com.whu.exam.modules.exam.entity;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.baomidou.mybatisplus.extension.activerecord.Model;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.whu.exam.modules.paper.enums.ExamState;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import org.springframework.format.annotation.DateTimeFormat;

import java.util.Date;

@Data
@TableName("el_exam")
public class Exam extends Model<Exam> {

    private static final long serialVersionUID = 1L;

    /**
     * ID
     */
    @ApiModelProperty(value = "ID", required=true)
    @TableId(value = "id", type = IdType.ASSIGN_ID)
    private String id;

    /**
     * 考试名称
     */
    @ApiModelProperty(value = "考试名称", required=true)
    private String title;

    /**
     * 考试描述
     */
    @ApiModelProperty(value = "考试描述", required=true)
    private String content;

    /**
     * 1公开2部门3定员
     */
    @ApiModelProperty(value = "1公开2部门3定员", required=true)
    @TableField("open_type")
    private Integer openType;

    /**
     * 组题方式1题库,2指定
     */
    @ApiModelProperty(value = "组题方式1题库,2指定", required=true)
    @TableField("join_type")
    private Integer joinType;

    /**
     * 难度:0不限,1普通,2较难
     */
    @ApiModelProperty(value = "难度:0不限,1普通,2较难", required=true)
    private Integer level;

    /**
     * 考试状态
     */
    @ApiModelProperty(value = "考试状态", required=true)
    private Integer state;

    /**
     * 是否限时
     */
    @ApiModelProperty(value = "是否限时", required=true)
    @TableField("time_limit")
    private Boolean timeLimit;

    /**
     * 开始时间
     */
    @JsonFormat(timezone = "GMT+8", pattern = "yyyy-MM-dd")
    @DateTimeFormat(pattern = "yyyy-MM-dd")
    @ApiModelProperty(value = "开始时间", required=true)
    @TableField("start_time")
    private Date startTime;

    /**
     * 结束时间
     */
    @JsonFormat(timezone = "GMT+8", pattern = "yyyy-MM-dd")
    @DateTimeFormat(pattern = "yyyy-MM-dd")
    @ApiModelProperty(value = "结束时间", required=true)
    @TableField("end_time")
    private Date endTime;

    /**
     * 创建时间
     */
    @ApiModelProperty(value = "创建时间", required=true)
    @TableField("create_time")
    private Date createTime;

    /**
     * 更新时间
     */
    @ApiModelProperty(value = "更新时间", required=true)
    @TableField("update_time")
    private Date updateTime;

    /**
     * 总分数
     */
    @ApiModelProperty(value = "总分数", required=true)
    @TableField("total_score")
    private Integer totalScore;

    /**
     * 总时长（分钟）
     */
    @ApiModelProperty(value = "总时长（分钟）", required=true)
    @TableField("total_time")
    private Integer totalTime;

    /**
     * 及格分数
     */
    @ApiModelProperty(value = "及格分数", required=true)
    @TableField("qualify_score")

    private Integer qualifyScore;
    public Integer getState(){

        if(this.timeLimit!=null && this.timeLimit){

            if(System.currentTimeMillis() < startTime.getTime() ){
                return ExamState.READY_START;
            }

            if(System.currentTimeMillis() > endTime.getTime()){
                return ExamState.OVERDUE;
            }

            if(System.currentTimeMillis() > startTime.getTime()
                    && System.currentTimeMillis() < endTime.getTime()
                    && !ExamState.DISABLED.equals(this.state)){
                return ExamState.ENABLE;
            }

        }

        return this.state;
    }
}
